﻿using System;

namespace BinarySearchTree
{
    public struct BinarySearchTree<T>
        where T : IComparable<T>
    {
        public TreeNode<T> root;

        public void Add(T element)
        {
            if (root == null)
            {
                root = new TreeNode<T>(element);
            }
            else
            {
                TreeNode<T> currentNode = root;
                while (currentNode.HasValue)
                {
                    if (element.CompareTo(currentNode.Value) <= 0)
                    {
                        currentNode = currentNode.LeftNode;
                    }
                    else
                    {
                        currentNode = currentNode.RightNode;
                    }
                }
                currentNode.Value = element;
            }
        }

        public bool Contains(T element)
        {
            TreeNode<T> currentNode = root;
            while (currentNode.HasValue)
            {
                if (element.CompareTo(currentNode.Value) < 0)
                {
                    currentNode = currentNode.LeftNode;
                }
                else if (element.CompareTo(currentNode.Value) > 0)
                {
                    currentNode = currentNode.RightNode;
                }
                else
                {
                    return true;
                }
            }
            return false;
        }
    }
}
